package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.MessageService;
import com.symone.webcms.model.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.*;

/**
 *   @description : 消息管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-13
 */
@RestController
@Api(value="/message", description="消息管理")
@RequestMapping("api/message")
public class MessageController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    public MessageService messageService;

    /**
     * @description : 获取消息分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:index")
    @RequestMapping(value = "/recievebox", method = RequestMethod.GET)
    @ApiOperation(value="获取消息分页列表", notes="获取消息分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="toId",value="接收者ID"),
            @ApiImplicitParam(name="isRead",value="是否已阅读"),
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage recieveBox( Long toId, Integer isRead, Integer pageSize, Integer pageNum) {
        if(pageSize == null){
            pageSize = 10;
        }
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        Page<MessageVO> page = new Page<MessageVO>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        if(toId == null) {
            toId = getSessionUser().getId();
        }
        if(isRead == null) {
            isRead = 0;
        }
        page = messageService.getMessageRecieveBoxPage(page, toId, isRead);
        return ResultMessage.success("获取消息的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取消息分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:index")
    @RequestMapping(value = "/sendbox", method = RequestMethod.GET)
    @ApiOperation(value="获取消息分页列表", notes="获取消息分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="fromId",value="发送者ID"),
            @ApiImplicitParam(name="isRead",value="是否已阅读"),
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage sendBox( Long fromId, Integer isRead, Integer pageSize, Integer pageNum) {
        Page<MessageVO> page = new Page<MessageVO>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        if(fromId == null) {
            fromId = getSessionUser().getId();
        }
        if(isRead == null) {
            isRead = 0;
        }
        page = messageService.getMessageSendBoxPage(page, fromId, isRead);
        return ResultMessage.success("获取消息的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有消息记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有消息信息", notes="获取所有消息信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="fromId",value="发送者ID"),
            @ApiImplicitParam(name="toId",value="接收者ID"),
            @ApiImplicitParam(name="isRead",value="是否已阅读")
    })
    public ResultMessage list(Long fromId, Long toId, Integer isRead) {
        Wrapper<Message> ew = new EntityWrapper<Message>();
        ew.where("status=1");

        if( fromId != null && fromId > 0 ){
            ew.and().eq("from_id",fromId);
        }
        if( toId != null && toId > 0 ){
            ew.and().eq("to_id",toId);
        }
        if( isRead != null && isRead > 0 ){
            ew.and().eq("is_read",isRead);
        }
        ew.orderBy("create_time",false);

        //logger.debug("SQL:"+ew.originalSql());

        return ResultMessage.success("获取所有消息信息",messageService.selectList(ew));
    }

    /**
     * @description : 通过id获取消息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取消息信息", notes="获取消息信息")
    @ApiImplicitParam(name="id",value="消息ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取消息信息成功!",messageService.selectById(id));
    }

    /**
     * @description : 添加消息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加消息信息", notes="添加消息信息")
    @ApiImplicitParam(name = "message", value = "消息信息", required = true )
    public ResultMessage add(Message message){
        messageService.insert(message);
        return ResultMessage.success("添加消息成功!");
    }

    /**
     * @description : 通过id逻辑删除消息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除消息", notes="逻辑删除消息")
    @ApiImplicitParam(name = "id", value = "消息Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(messageService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑消息删除成功！");
        } else {
            return ResultMessage.fail("逻辑消息删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除消息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除消息", notes="物理删除消息")
    @ApiImplicitParam(name = "id", value = "消息Id", required = true )
    public ResultMessage remove(long id){
        if(messageService.deleteById(id)) {
            return ResultMessage.success("物理删除消息成功！");
        } else {
            return ResultMessage.fail("物理删除消息失败！");
        }
    }

    /**
     * @description : 更新消息信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
     */
    @RequiresPermissions("cms:message:edit")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="更新消息信息", notes="更新消息信息")
    @ApiImplicitParam(name = "message", value = "消息信息", required = true )
    public ResultMessage update(Message message) {
        if(messageService.updateById(message)) {
            return ResultMessage.success("消息信息更新成功!");
        }else{
            return ResultMessage.fail("消息信息更新失败!");
        }
    }
    /**
     * @description : 批量物理删除消息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("cms:message:remove")
    @RequestMapping(value = "/batchRemove", method = RequestMethod.DELETE)
    public ResultMessage batchRemove(String ids){
        if(messageService.batchDelete(ids)) {
            return ResultMessage.success("批量删除成功！");
        } else {
            return ResultMessage.fail("批量删除失败！");
        }
    }

    /**
    * @description : 清除消息记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-13
    */
    @RequiresPermissions("sys:message:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除消息记录", notes="清除已被逻辑删除的消息记录")
    public ResultMessage clear(){
        int num = messageService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有消息记录被清除！");
        }
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public ResultMessage getMessageCount(){
        int count = messageService.selectCount(
                new EntityWrapper<Message>()
                .eq("to_id",getSessionUser().getId())
                .eq("is_read",0)
        );
        return ResultMessage.success("获取当前用户的新消息数",count);
    }

}
